package com.example.noelwhereismystuffs.views.AdminProfileUIActivity;

import com.example.noelproject.R;
import com.example.noelwhereismystuffs.models.AdminProfileModel;
import com.example.noelwhereismystuffs.models.Database;
import com.example.noelwhereismystuffs.models.User;
import com.example.noelwhereismystuffs.presenters.AdminProfilePresenter;
import com.example.noelwhereismystuffs.views.SignInActivity.SignInActivity;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;

public class AdminProfileUIActivity extends Activity implements IAdminProfileUIView{
	private AdminProfilePresenter myPresenter;
	@SuppressWarnings("unused")
	private User myUser;
	private Database myData;
	private static final String infoError = "Please enter all infomation.";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_admin_profile_ui);
		// Retrieve the Admin and Database from SignIn Activity
		myUser = (User) getIntent().getSerializableExtra("User");
		myData = (Database) getIntent().getSerializableExtra("Database");
		myPresenter = new AdminProfilePresenter(new AdminProfileModel(myData), this);
	}

	/**
	 * Handle click on Log Out button
	 * @param source
	 * 					get from the xml file
	 */
	public void onLogOutClick(View source) {
		// create an intent (a request for phone OS to do something)
		Intent intent = new Intent(this, SignInActivity.class);
		intent.putExtra("Database", myData);
		// now pass that to the phone OS and ask to start it
		startActivity(intent);
	}
	
	/**
	 * Handle Execute button
	 * @param source get from the xml file
	 */
	public void onExecuteClick(View source){
		// Get user's info
		String username = getUsernameField().getText().toString();
		String email = getEmailField().getText().toString();
		String password = getPasswordField().getText().toString();
		
		// Get button's information
		RadioButton userTypeRadioButton;
		RadioButton actionTypeRadioButton;
		RadioGroup userTypeRadioGroup = (RadioGroup) findViewById(R.id.UserTypes);
		RadioGroup actionRadioGroup = (RadioGroup) findViewById(R.id.Action);
		int selectedUserTypeButton = userTypeRadioGroup.getCheckedRadioButtonId();
		int selectedActionButton = actionRadioGroup.getCheckedRadioButtonId();
		userTypeRadioButton = (RadioButton) findViewById(selectedUserTypeButton);
		actionTypeRadioButton = (RadioButton) findViewById(selectedActionButton);
		String userType = userTypeRadioButton.getText().toString();
		String actionType = actionTypeRadioButton.getText().toString();
		Log.d("AdminProfileUIActivity", "actionType: " + actionType);
		Log.d("AdminProfileUIActivity", "userType: " + userType);
		
		// Check whether all info has been completely filled
		if (username.equals("") || email.equals("") || password.equals("")){
			getEmailField().requestFocus();
			getEmailField().setError(infoError);
			Log.d("AdminProfileUIActivity","Error");
		}else{
			// execute 
			myPresenter.onExecuteClick(userType, actionType, username, email, password);
			Log.d("AdminProfileUIActivity","Executing");
		}
		
	}
	
	/**
	 * Get information from Password Field
	 * @return
	 * 			EditText password
	 */
	private EditText getPasswordField() {
		return (EditText) findViewById(R.id.PasswordAdminUI);
	}

	/**
	 * Get information from Email Field
	 * @return
	 * 			EditText email
	 */
	private EditText getEmailField() {
		return (EditText) findViewById(R.id.EmailAdminUI);
	}
	
	/**
	 * Get information from Username Field
	 * @return
	 * 			EditText username
	 */
	
	private EditText getUsernameField(){
		return (EditText) findViewById(R.id.UsernameAdminUI);
	}

	@Override
	public void advance() {
		getUsernameField().requestFocus();
		getUsernameField().setError("Successful!");	
	}
	

}
